import { MessageBox,Message } from 'element-ui';
import router from "@/router";
import {getUserSnapshotOne, reqOrderMunCount, reqUpdateUserStatus} from "@/api";


const state = {
    UserSnapshotOne:{},
    OrderMumCount:{}

};
const actions = {
    ReceiveTip({commit},commGroupUser){
        console.log('commGroupUser',commGroupUser)
        if (commGroupUser.orderSnapshot.status === 1) {
            MessageBox.confirm(`<strong>您的求助订单【<span style="color: dodgerblue">${commGroupUser.orderSnapshot.title}</span>】已经被用户【<span style="color: green">${commGroupUser.orderSnapshot.helpUserName}</span>】接收，请尽快确认订单！</strong>`, '订单状态变更提示', {
                dangerouslyUseHTMLString: true,
                confirmButtonText: '现在就去',
                cancelButtonText: '稍后再去处理',
                type: 'warning',
                center: true
            }).then(() => {
                router.push({
                    name: 'receive',
                    query: {
                        snapshotId: commGroupUser.orderSnapshot.id,
                        orderInitiateCode: commGroupUser.orderSnapshot.orderInitiateCode
                    }
                })
                Message({
                    type: 'success',
                    message: '接收订单明细'
                });
            }).catch(() => {
                Message({
                    type: 'info',
                    message: '稍后请记得去处理喔！'
                });
            });
        }
        if (commGroupUser.orderSnapshot.status === 4) {
            MessageBox.confirm(`<strong>您的求助订单【<span style="color: dodgerblue">${commGroupUser.orderSnapshot.title}</span>】已经被用户【<span style="color: green">${commGroupUser.orderSnapshot.helpUserName}</span>】取消，是否继续？</strong>`, '订单状态变更提示', {
                dangerouslyUseHTMLString: true,
                confirmButtonText: '重新发布',
                cancelButtonText: '好的，我知道了',
                type: 'warning',
                center: true
            }).then(() => {
                router.push({
                    name: 'addOrder',
                    query: {
                        orderInitiateCode: commGroupUser.orderSnapshot.orderInitiateCode
                    }
                })
                Message({
                    type: 'success',
                    message: '重新发布'
                });
            }).catch(() => {
                Message({
                    type: 'info',
                    message: '稍后请记得去处理喔！'
                });
            });
        }
    },
    HelpReceiveTip({commit},commGroupUser) {
        console.log('commGroupUser', commGroupUser)
        if (commGroupUser.orderSnapshot.status === 2) {
            MessageBox.confirm(`<strong>您的帮助订单【<span style="color: dodgerblue">${commGroupUser.orderSnapshot.title}</span>】已经被对方确认，请按照约定时间完成帮助。</strong>`, '订单状态变更提示', {
                dangerouslyUseHTMLString: true,
                confirmButtonText: '查看订单详情',
                cancelButtonText: '确定',
                type: 'warning',
                center: true
            }).then(() => {
                router.push({
                    name: 'receive',
                    query: {
                        snapshotId: commGroupUser.orderSnapshot.id,
                        orderInitiateCode: commGroupUser.orderSnapshot.orderInitiateCode
                    }
                })
                Message({
                    type: 'success',
                    message: '接收订单明细'
                });
            }).catch(() => {
            });
        }
        if (commGroupUser.orderSnapshot.status === 3) {
            MessageBox.confirm(`<strong>您的帮助订单【<span style="color: dodgerblue">${commGroupUser.orderSnapshot.title}</span>】已经被对方取消，请看看其他需要帮助的人吧!。</strong>`, '订单状态变更提示', {
                dangerouslyUseHTMLString: true,
                confirmButtonText: '好的，我知道了',
                type: 'warning',
                center: true
            });
        }
    },
    async OrderMumCount({commit}){
        let resultOrderMumCount = await reqOrderMunCount()
        console.log('resultOrderMumCount')
        console.log(resultOrderMumCount)
        if (resultOrderMumCount.confirmOrderNum!==0 || resultOrderMumCount.cancelOrderNum !==0 || resultOrderMumCount.cancelHelpOrderNum !==0)
        MessageBox.confirm(`当前您存在未确认订单：<span style="color: red">${resultOrderMumCount.confirmOrderNum}</span>,被取消订单:<span style="color: blue">${resultOrderMumCount.cancelHelpOrderNum+resultOrderMumCount.cancelOrderNum}</span>`, '订单状态变更提示', {
            dangerouslyUseHTMLString: true,
            confirmButtonText: '现在就去',
            cancelButtonText: '稍后再去处理',
            type: 'warning',
            center: true
        }).then(() => {
            router.push('/myOrder/myInitiate')
            reqUpdateUserStatus();
        }).catch(() => {
            Message({
                type: 'info',
                message: '稍后请记得去处理喔！'
            });
        });
            commit("OrederMunCount",resultOrderMumCount)
    },
    // 主页订单快照信息显示
    async UserSnapshotOne({commit}){
        let resultUserSnapshotOne =  await getUserSnapshotOne()
        commit("UserSnapshotOne",resultUserSnapshotOne)
    }

};
const mutations = {

    UserSnapshotOne(state,result){
        state.UserSnapshotOne = result
    },
    OrederMunCount(state,resultOrderMumCount){
        state.OrderMumCount = resultOrderMumCount
    }

};
const getters = {};
export default {
    state,
    mutations,
    actions,
    getters
}
